package com.aruba.socketlibrary.protocol.SendManager;


import com.aruba.socketlibrary.utils.BCD;
import com.aruba.socketlibrary.utils.Tools;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import static com.aruba.socketlibrary.config.SerialportConfig.string_encode;


/**
 * Created by aruba on 2018/1/17.
 */
public class SendResponse {
    public byte commandId;
    public byte[] data;
    //true 不用等待应答
    public boolean isReply;

    public SendResponse(byte commandId, byte[] data) {
        this.commandId = commandId;
        this.data = data;
    }

    public SendResponse() {
    }

    public static SendResponse getReply(byte commandId, byte success) {
        SendResponse sendResponse = new SendResponse();
        byte[] bytes = new byte[4];
        bytes[0] = (byte) 0xFF;
        bytes[1] = commandId;
        bytes[2] = success;
        bytes[3] = 0x00;
        sendResponse.commandId = 0x01;
        sendResponse.data = bytes;
        sendResponse.isReply = true;

        return sendResponse;
    }

    /**
     * 心跳
     *
     * @return
     */
    public static SendResponse getHeart() {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x02;
        sendResponse.data = new byte[0];

        return sendResponse;
    }

    /**
     * 开始制作
     *
     * @return
     */
    public static SendResponse start(byte type) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x03;
        byte[] bytes = new byte[1];
        bytes[0] = type;
        sendResponse.data = bytes;

        return sendResponse;
    }

    /**
     * 签到
     *
     * @param Businessnumber    企业经营许可证号 byte[16]
     * @param certificateNumber 驾驶员从业资格证号 byte[19]
     * @param carNumber         车牌号 不包括汉字  byte[6]
     * @param time              签到时间  YYYYMMDDHHMM byte[6]
     * @return
     */
    public static SendResponse getLogin(String Businessnumber, String certificateNumber, String carNumber, String time) {
        SendResponse sendResponse = new SendResponse();
        ByteBuffer bb = ByteBuffer.allocate(47);
        bb.put(Businessnumber.getBytes());
        bb.position(16);
        bb.put(certificateNumber.getBytes());
        bb.position(35);
        bb.put(carNumber.getBytes());
        bb.position(41);
        bb.put(time.getBytes());

        sendResponse.commandId = 0x03;
        sendResponse.data = bb.array();

        return sendResponse;
    }

    /**
     * 签退
     *
     * @param Businessnumber    企业经营许可证号 byte[16]
     * @param certificateNumber 驾驶员从业资格证号 byte[19]
     * @param carNumber         车牌号 不包括汉字  byte[6]
     * @param time              签到时间  YYYYMMDDHHMM byte[6]
     * @return
     */
    public static SendResponse getUnLogin(String Businessnumber, String certificateNumber, String carNumber, String time) {
        SendResponse sendResponse = new SendResponse();
        ByteBuffer bb = ByteBuffer.allocate(47);
        bb.put(Businessnumber.getBytes());
        bb.position(16);
        bb.put(certificateNumber.getBytes());
        bb.position(35);
        bb.put(carNumber.getBytes());
        bb.position(41);
        bb.put(time.getBytes());

        sendResponse.commandId = 0x04;
        sendResponse.data = bb.array();
        sendResponse.isReply = true;

        return sendResponse;
    }

    /**
     * DVR 系统信息查询
     *
     * @param model   出租屏型号 BYTE[32] ASCII 字符，长度不足 32byte，右补 0x00
     * @param version 出租屏版本号 BYTE[32] ASCII 字符，长度不足 32byte，右补 0x00
     * @return
     */
    public static SendResponse getDVRInfo(String model, String version) {
        SendResponse sendResponse = new SendResponse();
        ByteBuffer bb = ByteBuffer.allocate(64);
        bb.put(model.getBytes());
        bb.position(32);
        bb.put(version.getBytes());

        sendResponse.commandId = 0x05;
        sendResponse.data = bb.array();

        return sendResponse;
    }

    /**
     * 红外遥控消息
     *
     * @param key     按键值  UINT8
     * @param up_down 按键状态 UINT8 0：放开；1：按下
     * @return
     */
    public static SendResponse getInfraredControl(byte key, byte up_down) {
        SendResponse sendResponse = new SendResponse();
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.put(key);
        bb.put(up_down);

        sendResponse.commandId = 0x06;
        sendResponse.data = bb.array();

        return sendResponse;
    }

    /**
     * 出租屏按键消息
     *
     * @param key     按键值  UINT8 1：F1;2:F2;3：F3;4：F4;5：F5
     * @param up_down 按键状态 UINT8 0：放开；1：按下
     * @return
     */
    public static SendResponse getKeyDown(byte key, byte up_down) {
        SendResponse sendResponse = new SendResponse();
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.put(key);
        bb.put(up_down);

        sendResponse.commandId = 0x07;
        sendResponse.data = bb.array();

        return sendResponse;
    }

    /**
     * 上行透传
     *
     * @param bytes 不定长度，一般不超过 1000byte
     * @return
     */
    public static SendResponse getTransparent(byte[] bytes) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x0F;
        sendResponse.data = bytes;
        sendResponse.isReply = true;

        return sendResponse;
    }

    /**
     * 事件报告
     *
     * @param id 事件 ID
     * @return
     */
    public static SendResponse getReport(int id) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x11;
        sendResponse.data = new byte[]{(byte) id};

        return sendResponse;
    }

    /**
     * 提问应答
     *
     * @param id       问题 ID UINT32
     * @param answerId 答案 ID UINT8
     * @return
     */
    public static SendResponse getAnswer(int id, int answerId) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x12;
        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        byteBuffer.put(Tools.intToByteArray(id));
        byteBuffer.put((byte) answerId);
        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }

    /**
     * 驾驶员抢答
     *
     * @param id 业务 ID UINT32
     * @return
     */
    public static SendResponse getOrder(int id) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x21;
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        byteBuffer.put(Tools.intToByteArray(id));
        sendResponse.data = byteBuffer.array();
        sendResponse.isReply = true;

        return sendResponse;
    }

    /**
     * 驾驶员电召任务完成确认
     *
     * @param id 业务 ID UINT32
     * @return
     */
    public static SendResponse getOrderComplite(int id) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x22;
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
        byteBuffer.put(Tools.intToByteArray(id));
        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }

    /**
     * 驾驶员取消订单
     *
     * @param id     业务 ID UINT32
     * @param reason 取消原因 UINT8 0：事故；1：路堵；2：其它
     * @return
     */
    public static SendResponse getOrderCancel(int id, int reason) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x23;
        ByteBuffer byteBuffer = ByteBuffer.allocate(5);
        byteBuffer.put(Tools.intToByteArray(id));
        byteBuffer.put((byte) reason);
        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }

    /**
     * 获取通话状态
     *
     * @return
     */
    public static SendResponse getCallStatus() {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x30;
        sendResponse.data = new byte[0];
        sendResponse.isReply = true;

        return sendResponse;
    }

    /**
     * 拨打电话
     *
     * @param phone_number 电话号码 STRING 电话号长度 3~20byte，一般不超过 11byte
     * @return
     */
    public static SendResponse getCall(String phone_number) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x31;
        try {
            sendResponse.data = phone_number.getBytes(string_encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return sendResponse;
    }

    /**
     * 接听电话
     *
     * @return
     */
    public static SendResponse getCallAnswer() {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x32;
        sendResponse.data = new byte[0];

        return sendResponse;
    }

    /**
     * 挂断电话
     *
     * @return
     */
    public static SendResponse getCallHangup() {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x33;
        sendResponse.data = new byte[0];

        return sendResponse;
    }

    /**
     * 拨打 DTMF
     *
     * @param number ASCII 字符，0 到 9 的数字键，’*’，’#’等
     * @return
     */
    public static SendResponse getDTMF(String number) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x34;
        sendResponse.data = number.getBytes();

        return sendResponse;
    }

    /**
     * 音量调整
     *
     * @param voice 音量大小 UINT8 音量等级，0 到 9 级（0 禁音，9 最高）
     * @return
     */
    public static SendResponse getAudio(int voice) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x35;
        sendResponse.data = new byte[]{(byte) voice};

        return sendResponse;
    }

    /**
     * 请求获取支付二维码图片
     *
     * @param businessnumber    企业经营许可证 号 BYTE[16] ASCII 字符，长度不足 16byte，右补 0x00
     * @param certificateNumber 驾驶员从业资格 证号 BYTE[19] ASCII 字符，长度不足 19byte，右补 0x00
     * @param carNumber         车牌号 BYTE[6] 车牌号，ASCII 字符，不包括汉字
     * @param flag              图片属性 UINT8 支付宝-0x01，微信-0x02
     * @return
     */
    public static SendResponse getQR(String businessnumber, String certificateNumber, String carNumber, int flag) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x36;
        ByteBuffer byteBuffer = ByteBuffer.allocate(42);
        byteBuffer.put(businessnumber.getBytes());
        byteBuffer.position(16);
        byteBuffer.put(certificateNumber.getBytes());
        byteBuffer.position(35);
        byteBuffer.put(carNumber.getBytes());
        byteBuffer.position(41);
        byteBuffer.put((byte) flag);

        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }

    /**
     * 支付记录信息上传
     *
     * @param GpsTime           GPS 时间 BCD[6] YYMMDDhhmmss
     * @param goInTime          上车时间 BCD[6] YY-MM-DD-hh-mm-ss，如有，请 填写，否则填 0
     * @param record            营运金额 BCD[3] 格式 XXXXX.X，单位为元，如 有，请填写，否则填 0
     * @param certificateNumber 从业资格证号 BYTE[19] 如有，请填写，否则填 0
     * @param url               支付二维码 URI 校验值
     * @return
     */
    public static SendResponse getPayRecord(String GpsTime, String goInTime, String record, String certificateNumber, String url) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x38;
        byte[] urlBytes = url.getBytes();
        ByteBuffer byteBuffer = ByteBuffer.allocate(34 + urlBytes.length);

        byteBuffer.put(BCD.str2Bcd(GpsTime));
        byteBuffer.position(6);
        byteBuffer.put(BCD.str2Bcd(goInTime));
        byteBuffer.position(12);
        byteBuffer.put(BCD.str2Bcd(record));
        byteBuffer.position(15);
        byteBuffer.put(certificateNumber.getBytes());
        byteBuffer.position(34);
        byteBuffer.put(urlBytes);

        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }

    /**
     * 上传刷卡 ID
     *
     * @param cardId 司机卡号 ID BYTE [32] 不足右补齐 0
     * @return
     */
    public static SendResponse getUploadCard(String cardId) {
        SendResponse sendResponse = new SendResponse();

        sendResponse.commandId = 0x35;
        ByteBuffer byteBuffer = ByteBuffer.allocate(32);
        byteBuffer.put(cardId.getBytes());
        sendResponse.data = byteBuffer.array();

        return sendResponse;
    }
}
